/**
 * form混合
 * */
import {
    mapState,
    mapMutations
} from 'vuex';
import {
    ApiRequest
} from '@api/basePage';
export default {
    data () {
        return {
            dynamicData: {}, // 配置的数据
            pagePath: '',
            grid: {
                xl: 8,
                lg: 8,
                md: 12,
                sm: 24,
                xs: 24
            },
            collapse: false, // 是否展开更多筛选条件
            formData: {}, // 表单提交数据
            copyFormData: {}, // copy form data 此处必须写
            copyFormDataRules: {}, // copy form rules 此处必须写
            requestApi: {}, // 页面所需接口集合
            uuid: '' // 唯一标识 访问页面必传
        }
    },
    computed: {
        ...mapState('admin/layout', ['isMobile']),
        ...mapState('admin/baseList', [
            'editPageData',
            'tableRowData'
        ]),
        labelWidth () {
            return this.isMobile ? undefined : 120;
        },
        labelPosition () {
            return this.isMobile ? 'top' : 'right';
        }
    },
    created () {
        this.uuid = this.$route.query.uuid;
        this.getConfigData();
    },
    methods: {
        ...mapMutations('admin/baseList', [
            'setEditPageData'
        ]),
        // // 获取该会员的基础信息
        getViewPageInfo () {
            let data = {
                uuid: this.uuid
            }
            // 开始请求会员信息
            ApiRequest(this.requestApi.getViewPageInfo.url, this.requestApi.getViewPageInfo.type, data).then(res => {
                this.$refs.formData.resetFields(); // 清空表单
                this.formData = res.data;
            })
        },
        // 组件内数据改变
        handleChangeFormData (data) {
            this.$set(this.$data, 'formData', data);
        },
        // 提交
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    let data = this.formData;
                    // 开始请求会员信息
                    ApiRequest(this.requestApi.getViewPageInfo.url, this.requestApi.getViewPageInfo.type, data).then(res => {
                        if (res.return_code === 0) {
                            this.$Message.success('成功!');
                            let jumpPath = this.dynamicData.basePath + 'List';
                            this.$router.push({
                                path: jumpPath
                            })
                        }
                    })
                } else {
                    this.$Message.error('Fail!');
                }
            })
        },
        // 清空表单
        handleReset (name) {
            // this.$refs[name].resetFields();
            this.getConfigData();
        },
        // 获取初始配置信息
        getConfigData () {
            this.pagePath = this.$route.path;
            let dynamicData = require('@/configFile/product/baseView/index').default[this.pagePath];
            this.requestApi = dynamicData.requestApi; // 页面所需接口集合
            this.$set(this.$data, 'dynamicData', dynamicData);
            this.setEditPageData(dynamicData);

            // copy 一份数据 方便清空表单数据
            let copyFormData = JSON.parse(JSON.stringify(this.dynamicData.formData));
            let copyFormDataRules = JSON.parse(JSON.stringify(this.dynamicData.formDataRules));
            // 传给子组件的数据
            this.copyFormData = copyFormData;
            this.copyFormDataRules = copyFormDataRules;

            // 重新获取数据
            this.uuid = this.$route.query.uuid;
            this.getViewPageInfo();
        }
    },
    watch: {
        '$route': 'getConfigData'
    }
}
